package pack0819;

import java.util.*;

public class Work {
    Scanner in = new Scanner(System.in);
    //1.输入一个字符串，输出一个字符串中的英文字母个数，数字个数
    public void work1(){
        System.out.println("请输入字符串：");
        String s = in.next();
        String s1= s.toLowerCase();
        byte[] bytes = s1.getBytes();
        int shu = 0, zi = 0;
        for(int i = 0; i<bytes.length;i++){
            if(bytes[i]>=97&&bytes[i]<=122){
                zi++;
            }else if(bytes[i]>=48&&bytes[i]<=57){
                shu++;
            }
        }
        System.out.println("这个字符串的英文字母个数为："+zi);
        System.out.println("这个字符串的数字个数为："+shu);
    }
    //2.输入一个字符串，输出在一个字符串中指定字符串出现的次数
    public void work2(){
        System.out.println("请输入字符串：");
        String s = in.next();
        System.out.println("请输入指定字符串：");
        String s2 = in.next();

       /* String s3 = s.replace(s2,"*");
        char[] chars = s3.toCharArray();
        int shu = 0;
        for(int i = 0; i<chars.length; i++){
            if(chars[i]=='*'){
                shu++;
            }
        }
        System.out.println("这个字符串的出现次数数为："+shu);*/
        String[] strings = s.split(s2);
        //System.out.println(Arrays.toString(strings));
        if (s.endsWith(s2)){
            System.out.println(strings.length);
        }else{
            System.out.println(strings.length-1);
        }

    }
    //3.双等号（==）和equals()方法判断两个字符串是否相等有什么区别（简单题）
    public void work3(){
        String s = "aaa";
        String s1 = "aaa";
        String s2 = new String("aaa");
        System.out.println(s==s1);
        System.out.println(s==s2);
        System.out.println("---------------");
        boolean b = s.equals(s1);;
        boolean b1 = s.equals(s2);
        System.out.println(b);
        System.out.println(b1);
    }
    //4.输入五种水果的英文名字（例如葡萄 grape、橘子 orange、香蕉 banana、苹果 apple、
    // 桃 peach），编写一个程序，输出这些水果的名称（按照在字典里的先后顺序输出）</u>
    public void work4(){
        System.out.println("请输入字符串：");
        String s = in.next();
        String[] strings = s.split(",");
        for(int i = 0; i<strings.length-1; i++){
            for(int j = 0; j<strings.length-1-i; j++){
                int s1 = strings[j].compareTo(strings[j+1]);
                if(s1>0){
                    String c = strings[j];
                    strings[j] = strings[j+1];
                    strings[j+1] = c;
                }
            }
        }
        System.out.println(Arrays.toString(strings));
    }
    //5.输入一个字符串，逆序输出
    public void work5(){
        System.out.println("请输入字符串：");
        String s = in.next();
        char[] chars = s.toCharArray();
        for(int i = chars.length-1; i>=0; i--){
            System.out.print(chars[i]);
        }
    }
    //6.给定一个原字符串A和他的长度，请返回逆序后的字符串。如输入："dog loves pig",13，
    // 返回："pig loves dog"。</u>
    public void work6(){
        String s = "dog loves pig";
        String[] strings = s.split(" ");
        System.out.println(Arrays.toString(strings));
        for(int i = strings.length-1; i>=0; i--){
            System.out.print(strings[i]+" ");
        }
    }
    //7.中国人的姓都是单个字，请随机输入一个人的姓名，然后输出姓和名
    public void work7(){
        System.out.println("请输入字符串：");
        String s = in.next();
        char[] chars = s.toCharArray();
        System.out.println("姓："+chars[0]);
        System.out.print("名：");
        for(int i = 1; i<chars.length; i++){
            System.out.print(chars[i]);
        }
    }
    //8.录入会员生日时，形式必须是‘月、日‘，例如’09/12’，录入的密码位数必须在6位到10位之间。
    // 允许用户重复录入，直到输入正确为止。</u>
    public void work8(){
        System.out.println("请输入生日月日，例如’09/12’");
        boolean b = true;
        while (b){
           String brithday = in.next();
           int i = brithday.indexOf("/");
           if(i>0){
               System.out.println("生日录入成功！");
               b = false;
           }else{
               System.out.println("生日输入非法！请重新输入");
           }

        }
        System.out.println("请输入密码：");
        boolean a = true;
        while (a){
            String s = in.next();
            if(s.length()>=6&&s.length()<=10){
                System.out.println("密码录入成功！");
                a = false;
            }else{
                System.out.println("密码录入失败，请重新输入！");
            }
        }
    }
    //9.输入一个字符串，判断是否回文
    public void work9(){
        System.out.println("请输入字符串：");
        String s = in.next();
        boolean a = true;
        for(int i = 0; i<s.length()/2; i++){
            if(s.charAt(i)!=s.charAt(s.length()-1-i)){
                a=false;
            }
        }
        if(a){
            System.out.println("回文");
        }else{
            System.out.println("不是回文");
        }
    }
    //<u>10.任意输入两个字符串a和b，如果a和b中出现的字符种类相同且每种字符出现的次数相同，
    // 则a和b互为变形词，请检查2个给定字符串是否互为变形词</u>
    public void work10(){
        String a = in.next();
        String b = in.next();
        char[] char1 = a.toCharArray();
        char[] char2 = b.toCharArray();
        List<Character> list1 = new ArrayList<>();
        List<Character> list2 = new ArrayList<>();
        for(int i = 0; i<a.length(); i++){
            list1.add(char1[i]);
        }
        for(int i = 0; i<b.length(); i++){
            list2.add(char2[i]);
        }
        Collections.sort(list1);
        Collections.sort(list2);
        boolean boo = true;
        for(int i = 0; i<a.length()&&i<b.length(); i++){
            if (list1.get(i)==list2.get(i)){

            }else{
                boo = false;
                break;
            }
        }
        if (boo){
            System.out.println("互为变形词");
        }else{
            System.out.println("不互为变形词");
        }

    }


    public static void main(String[] args) {
        new Work().work2();
    }
}
